import type {MenuDataItem} from "@ant-design/pro-layout";
import React from "react";
import * as allIcons from "@ant-design/icons"

const fixMenuItemIcon = (menus: MenuDataItem[], iconType = 'Outlined'): MenuDataItem[] => {
  menus.forEach( item => {
    const {icon, children} = item

    if (typeof icon === 'string') {
      const fixIconName = icon.slice(0, 1).toLocaleUpperCase() + icon.slice(1)
      item.icon = React.createElement(allIcons[fixIconName + iconType] || allIcons[icon])
      // eslint-disable-next-line @typescript-eslint/no-unused-expressions
      children && children.length > 0 ? item.children = fixMenuItemIcon(children) : null
    }
  })

  return menus
}

export default fixMenuItemIcon
